import MusicManager from "../../MusicManager";
import NoviceGuide from "../UI/NoviceGuide";
import MainHome from "../facility/priveteFacility/MainHome";
import { FacilityType } from "../info/GameEnum";
import { Global } from "../info/Global";
import PlayerManager from "../manager/PlayerManager";
import UiManager from "../manager/UiManager";
import ControlledPlayer from "../player/ControlledPlayer";
import Player from "../player/Player";
import Dialog from "./Dialog";

const { ccclass, property } = cc._decorator;

/**
 * 升级弹窗
 */
@ccclass
export default class MainHomeDialog extends Dialog {

    @property(cc.Label)
    levelLabel: cc.Label = null;

    @property(cc.Button)
    基地升级: cc.Button = null;

    @property(cc.Button)
    招募矿工: cc.Button = null;

    @property(cc.Button)
    采矿数量: cc.Button = null;

    @property(cc.Button)
    矿工速度: cc.Button = null;

    @property(cc.Button)
    炮弹血量: cc.Button = null;

    @property(cc.Button)
    建筑血量: cc.Button = null;

    @property(cc.Button)
    炮弹攻击: cc.Button = null;

    @property(cc.Button)
    炮弹射程: cc.Button = null;

    @property(cc.Button)
    建筑防御: cc.Button = null;

    @property(cc.Button)
    近防炮攻击: cc.Button = null;

    @property(cc.Button)
    维修基地: cc.Button = null;

    controllerPlayer: ControlledPlayer;

    protected onEnable(): void {
        this.controllerPlayer = PlayerManager.instance.controllerPlayer;
        this.controllerPlayer.node.on(Player.EventType.更新晶石, this.init, this);
        this.controllerPlayer.node.on(Player.EventType.更新石油, this.init, this);
    }

    protected onDisable(): void {
        cc.isValid(this.controllerPlayer) && this.controllerPlayer.node.targetOff(this);
    }

    public init() {
        let player = PlayerManager.instance.controllerPlayer
        let mainHomeInfo = Global.FacilityInfo[FacilityType.主基地];

        this.resetBtn(this.基地升级, mainHomeInfo.每等级升级要求, player.mainHomeLevel, null);
        this.levelLabel.string = (player.mainHomeLevel + 1).toString();

        this.resetBtn(this.招募矿工, [player.minerPrice], 0, null);
        let minerLimit = mainHomeInfo.每基地等级招募矿工上限[Math.min(player.mainHomeLevel, mainHomeInfo.每基地等级招募矿工上限.length - 1)];
        this.招募矿工.node.getChildByName("limit").getComponent(cc.Label).string = `(${(<MainHome>player.facilityArr[FacilityType.主基地][0]).minerArr.length}/${minerLimit})`;
        if ((<MainHome>player.facilityArr[FacilityType.主基地][0]).minerArr.length == minerLimit) {
            this.招募矿工.interactable = false;
        }

        this.resetBtn(this.采矿数量, mainHomeInfo.采矿数量.每等级升级要求, player.miningCountLevel, MainHomeDialog.format("采矿数量", player.miningCountLevel + 1));
        this.resetBtn(this.矿工速度, mainHomeInfo.矿工速度.每等级升级要求, player.minerSpeedLevel, MainHomeDialog.format("矿工速度", player.minerSpeedLevel + 1));
        this.resetBtn(this.炮弹血量, mainHomeInfo.炮弹血量.每等级升级要求, player.shellHealthBonusLevel, MainHomeDialog.format("炮弹血量", player.shellHealthBonusLevel + 1));
        this.resetBtn(this.建筑血量, mainHomeInfo.建筑血量.每等级升级要求, player.buildingHealthLevel, MainHomeDialog.format("建筑血量", player.buildingHealthLevel + 1));
        this.resetBtn(this.炮弹攻击, mainHomeInfo.炮弹攻击.每等级升级要求, player.shellDamageBonusLevel, MainHomeDialog.format("炮弹攻击", player.shellDamageBonusLevel + 1));
        this.resetBtn(this.炮弹射程, mainHomeInfo.炮弹射程.每等级升级要求, player.shellRangeBonusLevel, MainHomeDialog.format("炮弹射程", player.shellRangeBonusLevel + 1));
        this.resetBtn(this.建筑防御, mainHomeInfo.建筑防御.每等级升级要求, player.buildingDefenseLevel, MainHomeDialog.format("建筑防御", player.buildingDefenseLevel + 1));
        this.resetBtn(this.近防炮攻击, mainHomeInfo.近防炮攻击.每等级升级要求, player.artilleryDamageBonusLevel, MainHomeDialog.format("近防炮攻击", player.artilleryDamageBonusLevel + 1));


        this.resetBtn(this.维修基地, [mainHomeInfo.维修基地.维修要求], 0, MainHomeDialog.format("维修基地", null));
    }

    private resetBtn(btn: cc.Button, conditionList: { diamond: number, oil: number, level?: number }[], currlevel: number, anticipateInfo: string) {
        let player = PlayerManager.instance.controllerPlayer
        let diamondBalance = player.diamond;
        let oilBalance = player.oil;
        let priceNode = btn.node.getChildByName("price");
        let fullLevel = btn.node.getChildByName("fullLevel");
        let diamondNode = priceNode.getChildByName("diamond");
        let oilNode = priceNode.getChildByName("oil");
        let lockNode = btn.node.getChildByName("lock");
        let anticipate = btn.node.getChildByName("anticipate");

        let coolDownLabel = btn.node.getChildByName("coolDownLabel")?.getComponent(cc.Label);
        const cooldownTime = player.coolDownMap.get(btn.node.name);
        if (coolDownLabel) {
            coolDownLabel.node.active = !!cooldownTime;
            if (coolDownLabel.node.active) {
                coolDownLabel.string = `冷却中: ${Math.floor(cooldownTime)}s`;
            }
        }

        let price = conditionList[currlevel];
        let unlockMainHomeLevel = (price && price.level) ? price.level - 1 : 0;

        priceNode.active = player.mainHomeLevel >= unlockMainHomeLevel && currlevel < conditionList.length && !cooldownTime;
        if (priceNode.active) {
            diamondNode.active = price.diamond > 0;
            oilNode.active = price.oil > 0;
            diamondNode.getComponentInChildren(cc.Label).string = price.diamond.toString();
            oilNode.getComponentInChildren(cc.Label).string = price.oil.toString();
        }

        lockNode && (lockNode.active = player.mainHomeLevel < unlockMainHomeLevel, lockNode.getComponent(cc.Label).string = `基地${unlockMainHomeLevel + 1}级解锁`);

        fullLevel && (fullLevel.active = currlevel == conditionList.length && currlevel > 0);

        anticipate && (anticipate.getComponent(cc.Label).string = anticipateInfo);

        btn.interactable = priceNode.active && diamondBalance >= price.diamond && oilBalance >= price.oil;
    }

    onClick(event: cc.Event) {
        let player = PlayerManager.instance.controllerPlayer
        let name = event.target.name;
        MusicManager.instance.play(MusicManager.instance.btnMove);
        switch (name) {
            case "基地升级":
                player.MainHomeLevelUp();
                player.node.emit(NoviceGuide.EventType.主基地升级);
                this.init();
                break;
            case "招募矿工":
                let mainHomeInfo = Global.FacilityInfo[FacilityType.主基地];
                let mainHome = <MainHome>player.facilityArr[FacilityType.主基地][0];
                if (mainHome.minerArr.length < mainHomeInfo.每基地等级招募矿工上限[Math.min(player.mainHomeLevel, mainHomeInfo.每基地等级招募矿工上限.length - 1)]) {
                    mainHome.getComponent(MainHome).addMiner();
                    UiManager.instance.showTip("新增一个矿工！");
                    player.node.emit(NoviceGuide.EventType.主基地新增矿工);
                    this.init();
                }
                break;
            case "采矿数量":
                player.miningCountLevelUp();
                break;
            case "矿工速度":
                player.miningSpeedLevelUp();
                break;
            case "炮弹血量":
                player.shellHealthBonusLevelUp();
                break;
            case "建筑血量":
                player.buildingHealthLevelUp();
                break;
            case "炮弹攻击":
                player.shellDamageBonusLevelUp();
                break;
            case "炮弹射程":
                player.shellRangeBonusLevelUp();
                break;
            case "建筑防御":
                player.buildingDefenseLevelUp();
                break;
            case "近防炮攻击":
                player.artilleryDamageBonusLevelUp();
                break;
            case "维修基地":
                player.repairMainhome();
                break;
        }
    }

    public closeDialog(): void {
        this.node.active = false;
    }

    protected update(dt: number): void {
        let mainHomeInfo = Global.FacilityInfo[FacilityType.主基地];
        this.resetBtn(this.维修基地, [mainHomeInfo.维修基地.维修要求], 0, MainHomeDialog.format("维修基地", null));
    }

    private static format(name: keyof typeof Global.FacilityInfo[FacilityType.主基地], level: number): string {
        let mainHomeInfo = Global.FacilityInfo[FacilityType.主基地];

        if (mainHomeInfo[name]["每等级加成"]) {
            level = Math.min(level, mainHomeInfo[name]["每等级加成"].length - 1)
        }
        let ret: string;
        switch (name) {
            case '炮弹血量':
            case '建筑血量':
            case '炮弹射程':
            case '近防炮攻击':
                ret = `×${mainHomeInfo[name]["每等级加成"][level]}%`
                break;
            case '维修基地':
                ret = `×${mainHomeInfo[name]["维修比例"]}%`
                break;
            default:
                ret = `+${mainHomeInfo[name]["每等级加成"][level]}`
                break;
        }
        return ret;
    }

}
